<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .big{
            width: 1000px;
            position: relative;
            left: 0;
            right: 0;
            bottom: 0;
            top: 50px;
            margin: auto;
        }
        th,td{
            width: 250px;
            height: 30px;
            border: 1px solid black;
        }
        .btn{
            width: 100px;
            height: 30px;
            background-color: skyblue;
            border: 0;
            float: right;
        }
        .add-modal{
            display: none;
            width: 230px;
            height: 200px;
            background-color: antiquewhite;
            position: absolute;
            left: 50%;
            right: 0;
            margin-left: -115px;
            z-index: 11;
        }
        .xgmtk{
            display: none;
            width: 230px;
            height: 200px;
            background-color: antiquewhite;
            position: absolute;
            left: 50%;
            right: 0;
            margin-left: -115px;
            z-index: 11;
        }
        .zzc{
            display: none;
            position: fixed;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
            background: rgba(0, 0, 0, 0.3);
            z-index: 10;
        }
    </style>
</head>
<body>
    <div class="big">
        <h1>图书管理<button class="btn btn-primary plus-btn" data-bs-toggle="modal" data-bs-target=".add-modal">添加</button></h1>
        <table>
            <thead>
                <tr>
                    <th>序号</th>
                    <th>书名</th>
                    <th>作者</th>
                    <th>出版社</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody align="center">
                <!-- <tr>
                    <td>1</td>
                    <td>西游记</td>
                    <td>吴承恩</td>
                    <td>北京出版社</td>
                    <td><button class="sc" style="margin-right: 15px;">删除</button><button class="bj">编辑</button></td>
                </tr> -->
            </tbody>
        </table>
        <div class="add-modal">
            <form class="tjform">
                <div >
                    <h2>添加图书</h2>
                    书名:<input type="text" class="tjsm"><br>
                    作者:<input type="text" class="tjzz"><br>
                    出版社:<input type="text" class="tjcbs"><br>
                    <button class="tjqx">取消</button>
                    <button class="tjbtn">添加</button>
                </div>
            </form>
        </div>
        <div class="xgmtk">
            <form class="xgform">
                <h2>修改图书</h2>
            书名:<input type="text" class="xgsm"><br>
            作者:<input type="text" class="xgzz"><br>
            出版社:<input type="text" class="xgcbs"><br>
            <button class="xgqx">取消</button>
            <button class="xgbtn">修改</button>
            </form>
        </div>
        <div class="zzc"></div>
    </div>
    <script src="./lib/axios.min.js"></script>
    <script src="./lib/bootstrap.min.js"></script>
    <script src="./lib/form-serialize.js"></script>
</body>
</html>
<script>
    var yhm='abdsf1'
    function xr(){
        axios({
            url:"https://hmajax.itheima.net/api/books",
            params:{
                creator:yhm
            }
        }).then(res=>{
            var boolist=res.data.data
            var ymsj=boolist.map((ele,index)=>{
                return`
                <tr>
                    <td>${index+1}</td>
                    <td>${ele.bookname}</td>
                    <td>${ele.author}</td>
                    <td>${ele.publisher}</td>
                    <td data-id=${ele.id}><button class="sc" style="margin-right: 15px;">删除</button><button class="bj">编辑</button></td>
                </tr>
                `
            }).join('')
            document.querySelector('tbody').innerHTML=ymsj
        })
    }
    xr()
    //模态框
    var addmtk=document.querySelector('.add-modal')
    var addmodal=new bootstrap.Modal(addmtk)

    document.querySelector('.tjbtn').addEventListener('click',function(){
        var addForm=document.querySelector('.tjform')
        console.log(addForm);
        var bookObj=serialize(addForm,{hash:true,empty:true})
        axios({
            url:"https://hmajax.itheima.net/api/books",
            method:"POST",
            data:{
                ...bookObj,
                yhm
            }
        }).then(res=>{
            xr()
            addForm.reset()
            addmodal.hide()
        })
    })
    document.querySelector('body').addEventListener('click',function(event){
        console.log(event.target.className);
        
        if(event.target.className=='sc'){
            var sid=event.target.parentNode.dataset.id
            axios({
                url:`https://hmajax.itheima.net/api/books/${sid}`,
                method:"DELETE"
            }).then(res=>{
                xr()
            })
        }
    })
</script>